package com.biz.primus.ms.auth.dao.po;

import com.biz.primus.base.enums.CommonStatus;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

import com.biz.primus.ms.base.jpa.po.BaseEntity;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.NotBlank;

/**
 * 管理员
 *
 * @author david-liu
 * @date 2017年04月07日
 * @reviewer
 */
@Entity
@Table(name = "auth_user")
public class User extends BaseEntity {

    private static final long serialVersionUID = -4877271072444358347L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * 用户统一id
     */
    @Column(length = 32, nullable = false, unique = true)
    @NotNull(message = "用户名不能为空")
    @NotBlank(message = "用户名不能为空")
    private String userUnionId;

    /**
     * 身份描述
     */
    @Column(length = 100)
    private String description;

    /**
     * 状态
     */
    @Column
    @Enumerated(EnumType.STRING)
    private CommonStatus status = CommonStatus.ENABLE;

    /**
     * 所拥有的权限
     */
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "auth_user_role", joinColumns = {
            @JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {
            @JoinColumn(name = "role_id", referencedColumnName = "id")}, uniqueConstraints = {
            @UniqueConstraint(columnNames = {"user_id", "role_id"})})
    @Where(clause = "status='ENABLE'")
    private List<Role> roles;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * {@linkplain User#userUnionId}
     */
    public String getUserUnionId() {
        return userUnionId;
    }

    /**
     * {@linkplain User#userUnionId}
     */
    public void setUserUnionId(String userUnionId) {
        this.userUnionId = userUnionId;
    }

    /**
     * {@linkplain User#description}
     */
    public String getDescription() {
        return description;
    }

    /**
     * {@linkplain User#description}
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * {@linkplain User#status}
     */
    public CommonStatus getStatus() {
        return status;
    }

    /**
     * {@linkplain User#status}
     */
    public void setStatus(CommonStatus status) {
        this.status = status;
    }

    /**
     * {@linkplain User#roles}
     */
    public List<Role> getRoles() {
        return roles;
    }

    /**
     * {@linkplain User#roles}
     */
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}
